#include "func.h"
std::mutex mutex;
bool isOpen;
cv::Mat convertTo3Channels(const cv::Mat& binImg)
{
    cv::Mat three_channel = cv::Mat::zeros(binImg.rows,binImg.cols,CV_8UC3);
    std::vector<cv::Mat> channels;
    for (int i=0;i<3;i++)
    {
        channels.push_back(binImg);
    }
    merge(channels,three_channel);
    return three_channel;

}
void getFileNames(std::string path, std::vector<std::string>& files)
{
    struct dirent *ptr;
    DIR *dir;
    std::string PATH = path;
    dir=opendir(PATH.c_str());
    std::cout << "文件列表: "<< std::endl;
    while((ptr=readdir(dir))!=NULL)
    {

        //跳过'.'和'..'两个目录
        if(ptr->d_name[0] == '.')
            continue;
        //cout << ptr->d_name << endl;
        files.push_back(ptr->d_name);
    }

    for (int i = 0; i < files.size(); ++i)
    {
        std::cout << files[i] << std::endl;
    }
    std::cout<< "end of files"<<std::endl;
    closedir(dir);

	}
double calc_dis(double x1,double y1,double x2,double y2){
	double x = x1-x2;
	double y = y1-y2;
	return sqrt(x*x+y*y);	


	}
void cameraThreadFunc( cv::VideoCapture& capture,std::string& rtsp, cv::Mat& pFrame)
{

    capture.open(rtsp);
	isOpen=true;
    if (!capture.isOpened()) {
        isOpen = false;
        std::cout << "Failed to open camera with index " << rtsp << std::endl;
    }
	capture.set(cv::CAP_PROP_BUFFERSIZE,3);
    cv::Mat frame;
    while (isOpen) {
		while(true){
			struct timeval begin, end;
		    gettimeofday(&begin, 0);
		
			capture.grab();
		    // Stop measuring time and calculate the elapsed time
		    gettimeofday(&end, 0);
		    long seconds = end.tv_sec - begin.tv_sec;
		    long microseconds = end.tv_usec - begin.tv_usec;
		    double elapsed = seconds + microseconds*1e-6;
			if (elapsed>0.030)
				break;
			}
		capture.retrieve(frame);
        if (mutex.try_lock()) {
            frame.copyTo(pFrame);
            mutex.unlock();
        }
        cv::waitKey(30);
    }
    capture.release();
}

CMyContour::CMyContour(std::vector<cv::Point_<int>>contour, double x, double y,int num1){
	this->contour = contour;
	this->x = x;
	this->y = y;
	this->num=num1;
	}
void CArea::add_contour(CMyContour myContour){
	myContours.push_back(myContour);
	}
